package com.masterclient.app.businessmodule.minemodule.index.view;

import android.content.Context;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.eoner.baselibrary.bean.personal.CustomerOrderInfo;
import com.eoner.baselibrary.utils.GlideUtil;
import com.eoner.baselibrary.utils.ViewTransformUtil;
import com.eoner.baselibrary.bean.personal.MedalItemBean;
import com.masterclient.app.R;
import com.masterclient.app.businessmodule.minemodule.index.adapter.PersonMedalAdapter;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.List;

import jp.wasabeef.glide.transformations.CropCircleWithBorderTransformation;
import jp.wasabeef.glide.transformations.internal.Utils;

public class PersonHeaderView extends FrameLayout {

    /**
     * 头部状态
     * */
    @IntDef({PersonHeaderViewState.PERSON_HEAD_NO_LOGIN,PersonHeaderViewState.PERSON_HEAD_LOGIN_NO_DATA, PersonHeaderViewState.PERSON_HEAD_LOGIN_HAS_DATA})
    @Retention(RetentionPolicy.SOURCE)
    public @interface PersonHeaderViewState {
        //没有登录
        int PERSON_HEAD_NO_LOGIN = 0;
        //登录没有数据情况
        int PERSON_HEAD_LOGIN_NO_DATA = 1;
        //登录有数据情况
        int PERSON_HEAD_LOGIN_HAS_DATA = 2;
    }

    private ImageView mRoundImage;
    private TextView mUsername;
    private ImageView mVipView;
    private TextView mTipView;
    private RecyclerView mListView;
    private PersonMedalAdapter medalAdapter;
    private List<MedalItemBean>medalItemBeanList = new ArrayList<>();

    private PersonHeadViewListener iPersonHeadViewListener;

    private @PersonHeaderViewState int viewState;

    public void setPersonHeadViewListener(PersonHeadViewListener iPersonHeadViewListener) {
        this.iPersonHeadViewListener = iPersonHeadViewListener;
    }

    public PersonHeaderView(@NonNull Context context) {
        super(context);
        LayoutInflater.from(context).inflate(R.layout.layout_person_header,this);
        initUI();
    }

    public PersonHeaderView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.layout_person_header,this);
        initUI();
    }

    private void initUI() {
        mRoundImage = findViewById(R.id.roundImage);
        mUsername = findViewById(R.id.tv_username);
        mVipView = findViewById(R.id.iv_vip);
        mTipView = findViewById(R.id.person_tip_view);
        mListView = findViewById(R.id.medal_list_view);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mListView.setLayoutManager(linearLayoutManager);
        medalAdapter = new PersonMedalAdapter(getContext(),R.layout.item_person_medal,medalItemBeanList);
        mListView.setAdapter(medalAdapter);
        medalAdapter.setPersonMedalAdapterListener(new PersonMedalAdapter.PersonMedalAdapterListener() {
            @Override
            public void onClickItem(MedalItemBean itemBean) {
                if (iPersonHeadViewListener != null) {
                    iPersonHeadViewListener.onClickMedal(itemBean);
                }
            }
        });
    }

    public void refreshHeaderImage(String image) {
        ViewTransformUtil.glideImageView(getContext(), image, mRoundImage,
                new CropCircleWithBorderTransformation(Utils.toDp(2), 0xFFFFFF), R.mipmap.user_head_default_icon);

    }

    public void refreshHeaderImage(int image) {
        mRoundImage.setImageResource(image);
    }

    public void refreshNickName(String name) {
        if (name != null && name.length() > 0) {
            mUsername.setText(name);
        } else {
            mUsername.setText("");
        }
    }

    public void refreshVipImage(CustomerOrderInfo.DataBean dataBean) {
        if (!TextUtils.isEmpty(dataBean.getLevel_icon())) {
            mVipView.setVisibility(View.VISIBLE);
            GlideUtil.displayImage(getContext(), dataBean.getLevel_icon(), mVipView);
        } else {
            mVipView.setVisibility(View.GONE);
        }
    }

    public void setViewState(int viewState,String tips, List<MedalItemBean>medalItemBeanList) {
        this.viewState = viewState;
        switch (viewState) {
            case PersonHeaderViewState.PERSON_HEAD_LOGIN_HAS_DATA: {
                if (medalItemBeanList != null && medalItemBeanList.size() > 0) {
                    mTipView.setVisibility(GONE);
                    mListView.setVisibility(VISIBLE);
                    medalAdapter.getData().clear();
                    medalAdapter.addData(medalItemBeanList);
                } else {
                    mTipView.setVisibility(VISIBLE);
                    mListView.setVisibility(GONE);
                    if (tips != null && tips.length() > 0) {
                        mTipView.setText(tips);
                    } else {
                        mTipView.setText("好货只卖成本价，天天都是VIP");
                    }
                }
            }
                break;
            case PersonHeaderViewState.PERSON_HEAD_NO_LOGIN:
            case PersonHeaderViewState.PERSON_HEAD_LOGIN_NO_DATA: {
                mTipView.setText("好货只卖成本价，天天都是VIP");
                mTipView.setVisibility(VISIBLE);
                mListView.setVisibility(GONE);
                mVipView.setVisibility(GONE);
            }
                break;
        }
    }

    public interface PersonHeadViewListener {
        void onClickMedal(MedalItemBean itemBean);
    }
}
